package com.authine.cloudpivot.ext.domain.query;

import com.alibaba.fastjson.annotation.JSONField;
import com.authine.cloudpivot.ext.enums.goods.MallSortEnum;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.ObjToCollection;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.annotation.ColumnType;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Optional;

/**
 * @Author: zhoujian
 * @Date: 2019/12/24 8:52
 * @Version 1.0
 */
@Data
@Builder
@Api("商城商品查询")
@AllArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@RequiredArgsConstructor(staticName = "of")
public class MallQuery extends Page implements Serializable {
    private static final long serialVersionUID = 920310732714172952L;
    @ApiModelProperty(value = "属性值分页参数", dataType = "Integer")
    private Integer attrPage;
    @ApiModelProperty(value = "属性值分页参数", dataType = "Integer")
    private Integer attrLimit;
    @ApiModelProperty(value = "类型分页查询 0:名称 1:品牌 2:型号 3:规格", dataType = "Integer")
    private Integer attrType;
    @ApiModelProperty(value = "名称条件集合", dataType = "collection")
    @JSONField(deserializeUsing = ObjToCollection.class)
    private Collection<String> names;
    @ApiModelProperty(value = "品牌条件集合", dataType = "collection")
    @JSONField(deserializeUsing = ObjToCollection.class)
    private Collection<String> brands;
    @ApiModelProperty(value = "型号条件集合", dataType = "collection")
    @JSONField(deserializeUsing = ObjToCollection.class)
    private Collection<String> models;
    @JSONField(deserializeUsing = ObjToCollection.class)
    @ApiModelProperty(value = "规格条件集合", dataType = "collection")
    private Collection<String> specs;
    @ApiModelProperty(value = "分类", dataType = "collection")
    private String category;
    @ApiModelProperty(value = "商城商品类型 0: 普通商品 1: 精选商品", dataType = "Integer")
    private Integer mallFeatured;
    //    private Boolean hasStock;
    @ApiModelProperty(value = "排序 0: 价格从低到高 1:价格从高到底 为null时按默认排序", dataType = "Integer")
    private Integer sortType;
    @ApiModelProperty(value = "搜索框条件", dataType = "string")
    private String keyword;
    @ApiModelProperty(value = "商品id(多个id以空格分隔)", dataType = "string")
    private String goodsId;
    @ApiModelProperty(value = "商品id集合(与goodsId同时存在值时以goodsId为准)", dataType = "collection")
    @JSONField(deserializeUsing = ObjToCollection.class)
    private Collection<? extends Number> goodsIds;
    @ApiModelProperty(value = "商品类型(0:商城商品 1:单次采购 2:多次采购 3:虚拟商品)", dataType = "collection")
    @JSONField(deserializeUsing = ObjToCollection.class)
    private Collection<? extends Number> type;
    @ApiModelProperty(value = "商城商品审核状态(0:未审核 1:审核中 2:审核通过 3:审核不通过)", dataType = "Integer")
    private Integer reviewStatus;
    @ApiModelProperty(value = "供应商组织id", dataType = "string")
    private String vendorId;
    @ApiModelProperty(value = "供应商名称", dataType = "string")
    private String orgFullname;
    @ApiModelProperty(value = "分类", dataType = "string")
    private String catalog;
    @ApiModelProperty(value = "供应商sku", dataType = "string")
    private String vendorSku;
    @ApiModelProperty(value = "单位", dataType = "string")
    private String unit;
    @ApiModelProperty(value = "包装单位", dataType = "string")
    private String packageUnit;
    @ApiModelProperty(value = "包装数量", dataType = "string")
    private String packageNum;
    @ApiModelProperty(value = "供应商商品启禁用", dataType = "boolean")
    private Boolean enabled;
    @ApiModelProperty(value = "供应商商品上下架", dataType = "boolean")
    private Boolean saled;
    @ApiModelProperty(value = "商城中上下架", required = true, dataType = "boolean")
    private Boolean mallSales;
    @ApiModelProperty(value = "供应商商品特征启禁用", dataType = "boolean")
    private Boolean profileEnabled;
    @ApiModelProperty(value = "平台sku", dataType = "string")
    private String platformSku;
    @ApiModelProperty(value = "条形码", dataType = "string")
    private String barCode;
    @ApiModelProperty(value = "美国电子目录编码", dataType = "string")
    private String unspsc;
    @ApiModelProperty(value = "金税编码", dataType = "string")
    private String goldenTax;
    @ApiModelProperty(value = "过期时间", dataType = "date")
    @ColumnType(jdbcType = JdbcType.TIMESTAMP)
    private Date expiry;
    @ApiModelProperty(value = "供应商id集合", dataType = "collection")
    @JSONField(deserializeUsing = ObjToCollection.class)
    private Collection<? extends Number> vendorIds;
    @ApiModelProperty(value = "大于等于过期时间", dataType = "date")
    private Date gteExpiry;
    @ApiModelProperty(value = "大于等于价格", dataType = "double")
    private Double gtePrice;
    @ApiModelProperty(value = "小于等于价格", dataType = "double")
    private Double ltePrice;
    @ApiModelProperty(value = "大于等于交货期", dataType = "integer")
    private Integer gteDelivery;
    @ApiModelProperty(value = "小于等于交货期", dataType = "integer")
    private Integer lteDelivery;
    @ApiModelProperty(value = "大于等于创建时间", dataType = "date")
    private Date gteTime;
    @ApiModelProperty(value = "小于等于创建时间", dataType = "date")
    private Date lteTime;
    @ApiModelProperty(value = "供应商主要经营品牌", dataType = "collection")
    @JSONField(deserializeUsing = ObjToCollection.class)
    private Collection<String> majorBrands;

    private Integer gteMinOrder;
    private Integer lteMinOrder;

    private Boolean attrQuery;

    public void conditionPage(JdbcTemplateUtils query) {
        query.page(Optional.ofNullable(this.getAttrPage()).orElse(1), Optional.ofNullable(this.getAttrLimit()).orElse(20));
    }

    public void conditionSort(JdbcTemplateUtils query) {
        query.sort(MallSortEnum.idOf(this.getSortType()).sql);
    }

    public void condition(JdbcTemplateUtils query) {
        query.eq("enabled", this.enabled)
                .eq("profile_enabled", this.profileEnabled)
                .eq("saled", this.saled)
                .eq("mall_sales", this.mallSales)
                .in("type", type)
                .eq("review_status", this.reviewStatus)
                .in("name", names)
                .in("brand", brands)
                .in("model", models)
                .in("spec", specs)
                .in("category", category)
                .eq("mall_featured", mallFeatured)
                .gte("expiry", gteExpiry)
                .eq("vendor_id -> '$.id'", vendorId)
                .in("JSON_UNQUOTE(vendor_id -> '$.id')", vendorIds)
                .like("org_fullname", orgFullname)
                .like("catelog", catalog)
                .like("vendor_sku", vendorSku)
                .like("unit", unit)
                .like("package_unit", packageUnit)
                .eq("package_num", packageNum)
                .like("platform_sku", platformSku)
                .like("bar_code", barCode)
                .like("unspsc", unspsc)
                .like("golden_tax", goldenTax)
                .gte("price", gtePrice)
                .lte("price", ltePrice)
                .gte("price", gteDelivery)
                .gte("createdTime", gteTime)
                .lte("createdTime", lteTime)
                .in("brand", majorBrands)
                .gte("min_order", gteMinOrder)
                .lte("min_order", lteMinOrder);
        if (goodsId != null) {
            query.in("id", Arrays.asList(goodsId.split(" ")));
        }
    }


}
